package top.wecreate.buttermediaplayer;

import java.io.IOException;

import top.wecreate.buttermediaplayer.ButterMediaController.OnButterCompletionListener;
import top.wecreate.buttermediaplayer.ButterMediaController.OnButterPreparedListener;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {

	// public static String path1 = Environment.getExternalStorageDirectory()
	// + File.separator + "test.mp4";
	// public static String path2 = Environment.getExternalStorageDirectory()
	// + File.separator + "test.mp4";
	// public static String path3 = Environment.getExternalStorageDirectory()
	// + File.separator + "test.mp4";
	// public static String path4 = Environment.getExternalStorageDirectory()
	// + File.separator + "test.mp4";
	// public static String path5 = Environment.getExternalStorageDirectory()
	// + File.separator + "test.mp4";
	public static String path2 = "http://103.41.142.23/youku/656474C43134798251403739/030008010056D817F5CD4B003E88032C1A5077-247B-C7B0-118C-093B9800B970.mp4";
	public static String path1 = "http://103.41.140.19/youku/69777330B064C7DFB49BA4612/0300080100566FE9109388003E8803D5686CC7-3166-C1AE-E6B6-3223D36D8645.mp4";
	public static String path3 = "http://103.41.140.183/youku/676D5A8865407BBEC27346AD/030008010056D96EC37F0E003E880318A2B483-2982-94FE-236D-FF02B3301293.mp4";
	public static String path4 = "http://103.41.140.79/youku/67662A67CD3679E1697C2E21/030008010056B01DF8D164003E8803974495B1-9E64-C22C-0D52-5D9C26FA1970.mp4";
	public static String path5 = "http://103.41.140.82/youku/6572C2FCC9C4D7E2B15BD23ED/030008010056177B6A07E6003E8803DCF2E0A8-5249-ED75-A2C5-73B3CADEDBFA.mp4";
	SurfaceView surfaceView1;
	ButterMediaPlayer mediaPlayer1;
	SeekBar seekBar;
	TextView curPositon;
	TextView totDur;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		surfaceView1 = (SurfaceView) findViewById(R.id.surfaceView1);
		surfaceView1.getHolder().addCallback(new Callback() {

			@Override
			public void surfaceDestroyed(SurfaceHolder arg0) {

			}

			@Override
			public void surfaceCreated(SurfaceHolder arg0) {
				startNewPlay();
			}

			@Override
			public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
					int arg3) {

			}
		});

		findViewById(R.id.seek_one_end).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						mediaPlayer1.seekTo(14 * 1000);
					}
				});
		findViewById(R.id.seek_four_end).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						mediaPlayer1.seekTo(59 * 1000);
					}
				});
		findViewById(R.id.btn_start).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				mediaPlayer1.start();
			}
		});
		findViewById(R.id.btn_pause).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				mediaPlayer1.pause();
			}
		});
		findViewById(R.id.btn_resume).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				mediaPlayer1.resume();
			}
		});

		seekBar = (SeekBar) findViewById(R.id.seekbar);
		seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				mediaPlayer1.seekTo(seekBar.getProgress());
				seekBarTimer.sendEmptyMessageDelayed(0, 500);
			}

			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				seekBarTimer.removeMessages(0);
			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int arg1,
					boolean arg2) {
				curPositon.setText(formatDuration(seekBar.getProgress()));
			}
		});

		curPositon = (TextView) findViewById(R.id.curPos);
		totDur = (TextView) findViewById(R.id.totDur);

		findViewById(R.id.btn_restart).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						startNewPlay();
					}
				});
	}

	private void startNewPlay() {
		try {
			ButterMediaInfo[] infos = new ButterMediaInfo[5];
			ButterMediaInfo info0 = new ButterMediaInfo();
			info0.setPath(path1);
			info0.setDuration(15 * 1000);
			infos[0] = info0;

			ButterMediaInfo info1 = new ButterMediaInfo();
			info1.setPath(path2);
			info1.setDuration(15 * 1000);
			infos[1] = info1;

			ButterMediaInfo info2 = new ButterMediaInfo();
			info2.setPath(path3);
			info2.setDuration(30 * 1000);
			infos[2] = info2;

			ButterMediaInfo info3 = new ButterMediaInfo();
			info3.setPath(path4);
			info3.setDuration(15 * 1000);
			infos[3] = info3;

			ButterMediaInfo info4 = new ButterMediaInfo();
			info4.setPath(path5);
			info4.setDuration(5 * 1000);
			infos[4] = info4;

			mediaPlayer1 = new ButterMediaPlayer();
			mediaPlayer1.setDataSourceList(infos);
			mediaPlayer1.setDisplay(surfaceView1.getHolder());
			mediaPlayer1.setOnPreparedListener(new OnButterPreparedListener() {

				@Override
				public void onButterPrepared() {
					mediaPlayer1.start();
					seekBarTimer.sendEmptyMessageDelayed(0, 500);
				}
			});
			mediaPlayer1
					.setOnCompletionListener(new OnButterCompletionListener() {

						@Override
						public void onButterCompletion() {
							seekBarTimer.removeMessages(0);
						}
					});
			mediaPlayer1.prepareAsync();

		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private Handler seekBarTimer = new Handler() {
		public void handleMessage(android.os.Message msg) {
			seekBar.setMax(mediaPlayer1.getDuration());
			seekBar.setProgress(mediaPlayer1.getCurrentPosition());
			curPositon
					.setText(formatDuration(mediaPlayer1.getCurrentPosition()));
			totDur.setText(formatDuration(mediaPlayer1.getDuration()));
			seekBarTimer.sendEmptyMessageDelayed(0, 500);
		};
	};

	protected CharSequence formatDuration(int time) {
		int seconds = time / 1000;
		if (seconds < 60) {
			return "00:" + seconds;
		}

		int minutes = seconds / 60;
		seconds = seconds % 60;

		return minutes + ":" + seconds;
	}

}
